/**
 * Created by 肖浩容 on 2017/1/5.
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

import org.apache.http.impl.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.HttpEntity;
import org.apache.http.util.*;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpResponse;
import java.io.*;
public class Test {
    public static void main(String[] args) {
        //初始化计数器
        int sb = 0;
        //所有类别的tid值
        int tid[] = {22,26,126,127,24,25,47,27,33,32,153,51,152,28,31,30,59,29,54,130,20,154,156,17,65,136,19,121,37,124,122,39,96,95,98,138,21,76,75,161,162,163,157,158,164,159,166,71,137,131};
        //循环输出爬取到的信息
        for (int i = 0; i < 50; i++) {
            for (int j = 1; j <550; j++) {
                try {

                    HttpClient httpClient = HttpClients.createDefault();
                    String url = "http://api.bilibili.com/archive_rank/getarchiverankbypartion?callback=?&type=jsoup&tid=" + tid[i] + "&pn=" + j;
                    HttpGet req = new HttpGet(url);
                    req.addHeader("Accept", "application/json, text/javascript, */*;q=0.01");
                    req.addHeader("Accept-Encoding", "gzip,deflate,sdch");
                    req.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
                    req.addHeader("Content-Type", "text/html;charset=UTF-8");
                    req.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2057.400 QQBrowser/9.5.10158.400");
                    HttpResponse rep = httpClient.execute(req);
                    HttpEntity repEntity = rep.getEntity();
                    String content = EntityUtils.toString(repEntity);
                    //用json解析获取的jsoup内容
                    List imgCrawIdList1=new ArrayList();
                    List imgCrawIdList2=new ArrayList();
                    List imgCrawIdList3=new ArrayList();
                    List imgCrawIdList4=new ArrayList();
                    List imgCrawIdList5=new ArrayList();
                    JSONObject jo = new org.json.JSONObject(content.toString());
                    JSONObject jsonObject = jo.getJSONObject("data");
                    JSONObject jsonObject1 = jsonObject.getJSONObject("archives");
                    //将分别的内容储存到imgCrawIdList1-5的数组中
                    for (int k = 0; k <20 ; k++) {
                        imgCrawIdList1.add(jsonObject1.getJSONObject(String.valueOf(k)).get("aid"));
                        imgCrawIdList2.add(jsonObject1.getJSONObject(String.valueOf(k)).get("title"));
                        imgCrawIdList3.add(jsonObject1.getJSONObject(String.valueOf(k)).get("author"));
                        imgCrawIdList4.add(jsonObject1.getJSONObject(String.valueOf(k)).get("tname"));
                        imgCrawIdList5.add(jsonObject1.getJSONObject(String.valueOf(k)).get("favorites"));
                    }
                    Connection connection = Database.getConn();
                    PreparedStatement preparedStatement = null;
                    //按顺序将数组中的数据储存到数据库中
                    for(int x=0;x<20;x++){
                        //System.out.println(imgCrawIdList1.get(x));
                        //System.out.println(imgCrawIdList2.get(x));
                        //System.out.println(imgCrawIdList3.get(x));
                        //System.out.println(imgCrawIdList4.get(x));
                        //System.out.println(String.valueOf(imgCrawIdList5.get(x)));
                        Interface.Insert_sb(
                                String.valueOf(imgCrawIdList1.get(x)),
                                String.valueOf(imgCrawIdList2.get(x)),
                                String.valueOf(imgCrawIdList3.get(x)),
                                String.valueOf(imgCrawIdList4.get(x)),
                                Integer.parseInt(String.valueOf(imgCrawIdList5.get(x))),
                                connection,
                                preparedStatement
                        );
                        sb++;
                        System.out.println(sb);
                    }
                    Database.close(connection, null, preparedStatement, null);
                    //System.out.println(content);

                } catch (Exception e) {

                }

            }
        }
    }

}
